package at.socialconference.web.client.ui;

import java.util.List;

import at.socialconference.inter.model.User;

import com.google.gwt.cell.client.TextCell;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class PeopleTable extends VerticalPanel {

	ListDataProvider<User> listDataProvider;

	private CellTable<User> userTable;
	
	@SuppressWarnings("unused")
	private boolean requestFriendColumn;

	private static final ProvidesKey<User> KEY_PROVIDER = new ProvidesKey<User>() {
		public Object getKey(User user) {
			return user == null ? null : user.getId();
		}
	};

	private SingleSelectionModel<User> singleSelectionModel = new SingleSelectionModel<User>(KEY_PROVIDER);

	public PeopleTable() {
		this(true);
	}
	
	public PeopleTable(boolean requestFriendColumn) {
		this.requestFriendColumn = requestFriendColumn;
		userTable = new CellTable<User>(KEY_PROVIDER);
		userTable.setPageSize(10);
		userTable.setSelectionModel(singleSelectionModel);
		singleSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

			@Override
			public void onSelectionChange(SelectionChangeEvent event) {

				if (singleSelectionModel.getSelectedObject() instanceof User) {
					User user = (User) (singleSelectionModel.getSelectedObject());
					new UserPopup(user).show();
					singleSelectionModel.setSelected(singleSelectionModel.getSelectedObject(), false);
				}
			}
		});

		addColumns();
	}

	public void setData(List<User> users) {
		if (listDataProvider == null) {
			listDataProvider = new ListDataProvider<User>() {
				protected void onRangeChanged(HasData<User> display) {
					int start = display.getVisibleRange().getStart();
					int end = start + display.getVisibleRange().getLength();
					end = end >= getList().size() ? getList().size() : end;
					List<User> sub = getList().subList(start, end);
					updateRowData(start, sub);
				}
			};

			listDataProvider.addDataDisplay(userTable);
			SimplePager pager = new SimplePager();
			pager.setDisplay(userTable);

			add(userTable);
			add(pager);
		}

		listDataProvider.setList(users);

	}

	private void addColumns() {

		Column<User, String> columnTitle = new Column<User, String>(new TextCell()) {
			@Override
			public String getValue(User object) {
				return object.getTitle();
			}
		};

		Column<User, String> columnFirstName = new Column<User, String>(new TextCell()) {
			@Override
			public String getValue(User object) {
				return object.getFirstname();
			}
		};

		Column<User, String> columnLastName = new Column<User, String>(new TextCell()) {
			@Override
			public String getValue(User object) {
				return object.getLastname();
			}
		};
		
		Column<User, String> columnCountry = new Column<User, String>(new TextCell()) {
			@Override
			public String getValue(User object) {
				return object.getCountry();
			}
		};
		
		userTable.addColumn(columnTitle, "Title");
		userTable.addColumn(columnFirstName, "First Name");
		userTable.addColumn(columnLastName, "Last Name");
		userTable.addColumn(columnCountry, "Country");
		
		//TODO missing handler
//		if (requestFriendColumn==true) {
//			Column<User, String> columnAddAsFriend = new Column<User, String>(new ButtonCell()) {
//				@Override
//				public String getValue(User object) {
//					// TODO Auto-generated method stub
//					return "Add as friend";
//				}
//			};
//			userTable.addColumn(columnAddAsFriend);
//		}
	}
}
